//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "AverageBMALeg.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Times/Schedule.h>
#include <gen/QL/Indexes/BMAIndex.h>
#include <gen/QL/CashFlow.h>
#include <gen/QL/Times/DayCounter.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CAverageBMALeg::CAverageBMALeg (Cephei::QL::Times::ISchedule^ schedule, Cephei::QL::Indexes::IBMAIndex^ index) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CSchedule^ _Cschedule;
    CBMAIndex^ _Cindex;
    try
    {
#ifdef HANDLE
        _phAverageBMALeg = NULL;
#endif
        _Cschedule = safe_cast<CSchedule^> (schedule);
        _Cschedule->Lock();
        QuantLib::Schedule& _schedule = static_cast<QuantLib::Schedule&> (_Cschedule->GetReference ()); 
        _Cindex = safe_cast<CBMAIndex^> (index);
        _Cindex->Lock();
        boost::shared_ptr<QuantLib::BMAIndex>& _index = static_cast<boost::shared_ptr<QuantLib::BMAIndex>&> (_Cindex->GetShared ()); 
        _ppAverageBMALeg = new boost::shared_ptr<QuantLib::AverageBMALeg> (new QuantLib::AverageBMALeg ( _schedule,  _index ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cschedule != nullptr) _Cschedule->Unlock();
        if (_Cindex != nullptr) _Cindex->Unlock();
    }
}
Cephei::QL::Cashflows::CAverageBMALeg::CAverageBMALeg (boost::shared_ptr<QuantLib::AverageBMALeg>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAverageBMALeg = NULL;
#endif
	_ppAverageBMALeg = &childNative;
    
}
Cephei::QL::Cashflows::CAverageBMALeg::CAverageBMALeg (QuantLib::AverageBMALeg& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phAverageBMALeg = NULL;
#endif
	_ppAverageBMALeg = new boost::shared_ptr<QuantLib::AverageBMALeg> (&childNative);
    
    _AverageBMALegOwner = owner;
    
}

Cephei::QL::Cashflows::CAverageBMALeg::CAverageBMALeg (CAverageBMALeg^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAverageBMALeg = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAverageBMALeg = new boost::shared_ptr<QuantLib::AverageBMALeg> (copy->GetShared());
        
    }
}
Cephei::QL::Cashflows::CAverageBMALeg::CAverageBMALeg (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAverageBMALeg = NULL;
#endif
	if (!t->IsSubclassOf(CAverageBMALeg::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CAverageBMALeg::CAverageBMALeg (QuantLib::Handle<QuantLib::AverageBMALeg>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAverageBMALeg = &childNative;
	_ppAverageBMALeg = &static_cast<boost::shared_ptr<QuantLib::AverageBMALeg>>(childNative.currentLink());
    
    _AverageBMALegOwner = owner;
}
Cephei::QL::Cashflows::CAverageBMALeg::CAverageBMALeg (QuantLib::Handle<QuantLib::AverageBMALeg> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAverageBMALeg = &childNative;
	_ppAverageBMALeg = &static_cast<boost::shared_ptr<QuantLib::AverageBMALeg>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CAverageBMALeg::CAverageBMALeg (QuantLib::AverageBMALeg childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAverageBMALeg = NULL;
#endif
	_ppAverageBMALeg = new boost::shared_ptr<QuantLib::AverageBMALeg> (new QuantLib::AverageBMALeg (childNative));
    
}
#endif

Cephei::QL::Cashflows::CAverageBMALeg::~CAverageBMALeg ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppAverageBMALeg != NULL)
    {
	    delete _ppAverageBMALeg;
        _ppAverageBMALeg = NULL;
    }
}
Cephei::QL::Cashflows::CAverageBMALeg::!CAverageBMALeg ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppAverageBMALeg != NULL)
    {
	    delete _ppAverageBMALeg;
    }
}
QuantLib::AverageBMALeg& Cephei::QL::Cashflows::CAverageBMALeg::GetReference ()
{
    if (_ppAverageBMALeg == NULL) throw REFNEW NativeNullException ();
	return **_ppAverageBMALeg;
}
boost::shared_ptr<QuantLib::AverageBMALeg>& Cephei::QL::Cashflows::CAverageBMALeg::GetShared ()
{
    if (_ppAverageBMALeg == NULL) throw REFNEW NativeNullException ();
	return *_ppAverageBMALeg;
}
QuantLib::AverageBMALeg* Cephei::QL::Cashflows::CAverageBMALeg::GetPointer ()
{
    if (_ppAverageBMALeg == NULL) throw REFNEW NativeNullException ();
	return &**_ppAverageBMALeg;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AverageBMALeg>& Cephei::QL::Cashflows::CAverageBMALeg::GetHandle ()
{
	if (_phAverageBMALeg == NULL)
	{
		_phAverageBMALeg = new Handle<QuantLib::AverageBMALeg> (*_ppAverageBMALeg);
	}
	return *_phAverageBMALeg;
}
#endif
bool Cephei::QL::Cashflows::CAverageBMALeg::HasNative () 
{
	return (_ppAverageBMALeg != NULL);
}

Cephei::QL::Cashflows::IAverageBMALeg^ Cephei::QL::Cashflows::CAverageBMALeg::WithGearings (Cephei::Core::IVector<Double>^ gearings)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        gearings->Lock();
        INativeVector<Double>^ _NCIgearings = gearings->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCgearings = safe_cast<CDoubleVector^>(_NCIgearings);
        std::vector<QuantLib::Real>& _gearings = static_cast<std::vector<QuantLib::Real>&> (_NCgearings->GetReference ());
    	QuantLib::AverageBMALeg& _rv = (QuantLib::AverageBMALeg&)(*_ppAverageBMALeg)->withGearings ( _gearings );   
        Cephei::QL::Cashflows::CAverageBMALeg^ _nrv = REFNEW Cephei::QL::Cashflows::CAverageBMALeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (gearings != nullptr) gearings->Unlock(); //NO2  
    }
}
Cephei::QL::Cashflows::IAverageBMALeg^ Cephei::QL::Cashflows::CAverageBMALeg::WithNotionals (Cephei::Core::IVector<Double>^ notionals)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        notionals->Lock();
        INativeVector<Double>^ _NCInotionals = notionals->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCnotionals = safe_cast<CDoubleVector^>(_NCInotionals);
        std::vector<QuantLib::Real>& _notionals = static_cast<std::vector<QuantLib::Real>&> (_NCnotionals->GetReference ());
    	QuantLib::AverageBMALeg& _rv = (QuantLib::AverageBMALeg&)(*_ppAverageBMALeg)->withNotionals ( _notionals );   
        Cephei::QL::Cashflows::CAverageBMALeg^ _nrv = REFNEW Cephei::QL::Cashflows::CAverageBMALeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (notionals != nullptr) notionals->Unlock(); //NO2  
    }
}
Cephei::QL::Cashflows::IAverageBMALeg^ Cephei::QL::Cashflows::CAverageBMALeg::WithPaymentAdjustment (QL::Times::BusinessDayConventionEnum convention)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::BusinessDayConvention _convention = (QuantLib::BusinessDayConvention)convention ;
    	QuantLib::AverageBMALeg& _rv = (QuantLib::AverageBMALeg&)(*_ppAverageBMALeg)->withPaymentAdjustment ( _convention );   
        Cephei::QL::Cashflows::CAverageBMALeg^ _nrv = REFNEW Cephei::QL::Cashflows::CAverageBMALeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::IAverageBMALeg^ Cephei::QL::Cashflows::CAverageBMALeg::WithPaymentDayCounter (Cephei::QL::Times::IDayCounter^ dayCounter)
{
    CDayCounter^ _CdayCounter;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
    	QuantLib::AverageBMALeg& _rv = (QuantLib::AverageBMALeg&)(*_ppAverageBMALeg)->withPaymentDayCounter ( _dayCounter );   
        Cephei::QL::Cashflows::CAverageBMALeg^ _nrv = REFNEW Cephei::QL::Cashflows::CAverageBMALeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Cashflows::IAverageBMALeg^ Cephei::QL::Cashflows::CAverageBMALeg::WithSpreads (Cephei::Core::IVector<Double>^ spreads)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        spreads->Lock();
        INativeVector<Double>^ _NCIspreads = spreads->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCspreads = safe_cast<CDoubleVector^>(_NCIspreads);
        std::vector<QuantLib::Spread>& _spreads = static_cast<std::vector<QuantLib::Spread>&> (_NCspreads->GetReference ());
    	QuantLib::AverageBMALeg& _rv = (QuantLib::AverageBMALeg&)(*_ppAverageBMALeg)->withSpreads ( _spreads );   
        Cephei::QL::Cashflows::CAverageBMALeg^ _nrv = REFNEW Cephei::QL::Cashflows::CAverageBMALeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (spreads != nullptr) spreads->Unlock(); //NO2  
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::IAverageBMALeg^ Cephei::QL::Cashflows::CAverageBMALeg_Factory::Create (Cephei::QL::Times::ISchedule^ schedule, Cephei::QL::Indexes::IBMAIndex^ index)
{
    return REFNEW CAverageBMALeg ( schedule,  index);
}
